package com.fr.base.dao;

import com.fr.data.core.db.DBUtils;
import com.fr.data.core.db.dialect.Dialect;
import com.fr.data.core.db.dialect.DialectFactory;
import com.fr.data.core.db.dml.Delete;
import com.fr.data.core.db.dml.Insert;
import com.fr.data.core.db.dml.Table;
import com.fr.data.core.db.dml.Update;
import com.fr.data.impl.DatabaseConnection;
import java.io.PrintStream;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcTemplate
{
  private DatabaseConnection databaseConnection;

  public JdbcTemplate(DatabaseConnection paramDatabaseConnection)
  {
    this.databaseConnection = paramDatabaseConnection;
  }

  public void execute(String paramString)
    throws Exception
  {
    execute(new StatementCallback(this, paramString)
    {
      private final String val$sql;
      private final JdbcTemplate this$0;

      public Object doInStatement()
        throws SQLException
      {
        paramStatement.execute(this.val$sql);
        return null;
      }

      public String getSql()
      {
        return this.val$sql;
      }
    });
  }

  public Object query(String paramString, ResultSetExtractor paramResultSetExtractor)
    throws Exception
  {
    return execute(new StatementCallback(this, paramString, paramResultSetExtractor)
    {
      private final String val$sql;
      private final ResultSetExtractor val$rse;
      private final JdbcTemplate this$0;

      public Object doInStatement()
        throws SQLException
      {
        Object localObject1;
        ResultSet localResultSet1 = null;
        try
        {
          localResultSet1 = paramStatement.executeQuery(this.val$sql);
          ResultSet localResultSet2 = localResultSet1;
          localObject1 = this.val$rse.extractData(localResultSet2);
          return localObject1;
        }
        catch (Exception localException)
        {
          localException.printStackTrace();
          DBUtils.closeResultSet(localResultSet1);
          localObject1 = null;
          return localObject1;
        }
        finally
        {
          DBUtils.closeResultSet(localResultSet1);
        }
      }

      public String getSql()
      {
        return this.val$sql;
      }
    });
  }

  public Object execute(StatementCallback paramStatementCallback)
    throws Exception
  {
    Statement localStatement2;
    Connection localConnection1 = this.databaseConnection.createConnection();
    if (localConnection1.isClosed())
      System.out.println(localConnection1.isClosed());
    Statement localStatement1 = null;
    try
    {
      Connection localConnection2 = localConnection1;
      localStatement1 = localConnection2.createStatement();
      localStatement2 = localStatement1;
      Object localObject1 = paramStatementCallback.doInStatement(localStatement2);
      Object localObject2 = localObject1;
      return localObject2;
    }
    catch (SQLException localSQLException)
    {
      localSQLException.printStackTrace();
      DBUtils.closeStatement(localStatement1);
      localStatement1 = null;
      DBUtils.closeConnection(localConnection1);
      localConnection1 = null;
      localStatement2 = null;
      return localStatement2;
    }
    finally
    {
      DBUtils.closeStatement(localStatement1);
      localStatement1 = null;
      DBUtils.closeConnection(localConnection1);
      localConnection1 = null;
    }
  }

  public boolean execute(Insert paramInsert)
  {
    Connection localConnection = null;
    PreparedStatement localPreparedStatement = null;
    ResultSet localResultSet = null;
    try
    {
      localConnection = this.databaseConnection.createConnection();
      localPreparedStatement = paramInsert.createPreparedStatement(localConnection);
      localPreparedStatement.execute();
      int i = 1;
      return i;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
      DBUtils.closeResultSet(localResultSet);
      DBUtils.closeStatement(localPreparedStatement);
      DBUtils.closeConnection(localConnection);
      int j = 0;
      return j;
    }
    finally
    {
      DBUtils.closeResultSet(localResultSet);
      DBUtils.closeStatement(localPreparedStatement);
      DBUtils.closeConnection(localConnection);
    }
  }

  public Serializable execute(Insert paramInsert, boolean paramBoolean)
  {
    Connection localConnection = null;
    PreparedStatement localPreparedStatement1 = null;
    ResultSet localResultSet = null;
    try
    {
      localConnection = this.databaseConnection.createConnection();
      Dialect localDialect = DialectFactory.generateDialect(localConnection);
      boolean bool = localConnection.getAutoCommit();
      localDialect.setAutoCommit(localConnection, false);
      localPreparedStatement1 = paramInsert.createPreparedStatement(localConnection);
      if (localPreparedStatement1 == null)
      {
        localObject2 = new Long(0L);
        return localObject2;
      }
      localPreparedStatement1.execute();
      Object localObject2 = null;
      if (paramBoolean)
      {
        localObject3 = localDialect.getIdentitySelectString(paramInsert.getTable().getName(), "id", -5);
        PreparedStatement localPreparedStatement2 = localConnection.prepareStatement((String)localObject3);
        localResultSet = localPreparedStatement2.executeQuery();
        localObject2 = getIdentifier(localResultSet, Long.class);
        localPreparedStatement2.close();
      }
      localConnection.commit();
      localDialect.setAutoCommit(localConnection, bool);
      Object localObject3 = localObject2;
      return localObject3;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
      DBUtils.closeStatement(localPreparedStatement1);
      DBUtils.closeConnection(localConnection);
      Object localObject1 = null;
      return localObject1;
    }
    finally
    {
      DBUtils.closeStatement(localPreparedStatement1);
      DBUtils.closeConnection(localConnection);
    }
  }

  public boolean execute(Update paramUpdate)
  {
    Connection localConnection = null;
    PreparedStatement localPreparedStatement = null;
    ResultSet localResultSet = null;
    try
    {
      localConnection = this.databaseConnection.createConnection();
      localPreparedStatement = paramUpdate.createPreparedStatement(localConnection);
      if (localPreparedStatement == null)
      {
        i = 0;
        return i;
      }
      localPreparedStatement.execute();
      int i = 1;
      return i;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
      DBUtils.closeResultSet(localResultSet);
      DBUtils.closeStatement(localPreparedStatement);
      DBUtils.closeConnection(localConnection);
      int j = 0;
      return j;
    }
    finally
    {
      DBUtils.closeResultSet(localResultSet);
      DBUtils.closeStatement(localPreparedStatement);
      DBUtils.closeConnection(localConnection);
    }
  }

  public boolean execute(Delete paramDelete)
  {
    Connection localConnection = null;
    PreparedStatement localPreparedStatement = null;
    ResultSet localResultSet = null;
    try
    {
      localConnection = this.databaseConnection.createConnection();
      localPreparedStatement = paramDelete.createPreparedStatement(localConnection);
      if (localPreparedStatement == null)
      {
        i = 0;
        return i;
      }
      localPreparedStatement.execute();
      int i = 1;
      return i;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
      DBUtils.closeResultSet(localResultSet);
      DBUtils.closeStatement(localPreparedStatement);
      DBUtils.closeConnection(localConnection);
      int j = 0;
      return j;
    }
    finally
    {
      DBUtils.closeResultSet(localResultSet);
      DBUtils.closeStatement(localPreparedStatement);
      DBUtils.closeConnection(localConnection);
    }
  }

  private static Serializable getIdentifier(ResultSet paramResultSet, Class paramClass)
    throws SQLException
  {
    if (!(paramResultSet.next()))
      throw new RuntimeException("No Idetity");
    if (paramClass == Long.class)
      return new Long(paramResultSet.getLong(1));
    if (paramClass == Integer.class)
      return new Integer(paramResultSet.getInt(1));
    if (paramClass == Short.class)
      return new Short(paramResultSet.getShort(1));
    if (paramClass == String.class)
      return paramResultSet.getString(1);
    throw new RuntimeException("this id generator generates long, integer, short or string");
  }
}